<?php
class QSource_System_Models_ArticleCat extends QSource_System_Models_System {
	public $_name = "article_cat";
	protected $_sequence = true;
	/*
	public function __construct(){
		$config = new Zend_Config_Ini(APPLICATION_PATH . '/configs/application.ini', APPLICATION_ENV);
		$prefix = $config->resources->db->prefix;
		if($prefix!='')
			$this->_name = $prefix.'_'.$this->_name;		
	}*/
	public function getAll() {
		$result = $this->select();
		return $this->fetchAll($result)->toArray();
	}
	public function getAllRootCatActive(){
		$result = $this->select();
		$result->where('status = 1');
		$result->where('parent_id = 1');
		return $this->fetchAll($result)->toArray();
	}
	public function getAllLeapCatActive(){
		$result = $this->select();
		$result->where('status = 1');
		$result->where('parent_id != 1');
		return $this->fetchAll($result)->toArray();
	}
	public function getAliasById($id){
		$result = $this->select();
		$result->where('id = ?',$id);
		$data = $this->fetchRow($result)->toArray();
		return $data['alias'];
	}
	public function getItemId($id){
		$result = $this->select();
		$result->where('id = ?',$id);
		return $this->fetchRow($result)->toArray();
	}
	public function getItemAlias($alias)
	{
		$result = $this->select();
		$result->where('alias = ?',$alias);
		return $this->fetchRow($result)->toArray();
	}
	static function getNamebyId($id)
	{
		$excute = new QSource_System_Models_ArticleCat();
		$query = $excute->select();
		$query->where('id = ?',$id);
		$result = $excute->fetchRow($query)->toArray();
		return $result['name'];
	}
	public function getItemsLanguage($lang){
		$result = $this->select();
		$result->where('language = ?',$lang);
		return $this->fetchAll($result)->toArray();
	}
	public function checkAlias($alias)
	{
		$query = $this->select();
		$query->where('alias = ?',$alias);
		$result = $this->fetchAll($query)->toArray();
		if(count($result)!=0)
			return true;
		else
			return false;
	}
	public function changeStatus($id)
	{
		$query = $this->select();
		$query->where('id = ?',$id);
		$result = $this->fetchRow($query)->toArray();
		if($result['status']=='1')
		{
			$data = array('status'=>'0'); 
			$this->update($data, 'id = '.$id);
		}
		else
		{
			$data = array('status'=>'1'); 
			$this->update($data, 'id = '.$id);
		}
	}
	public function checkImage($image)
	{
		$query = $this->select();
		$query->where('image = ?',$image);
		$result = $this->fetchAll($query)->toArray();
		if(count($result)!=0)
			return true;
		else
			return false;
	}
	public function insertItem($data)
	{
		try {
		    $this->insert($data);
		} 
		catch (Exception $e) {
		    return $e->getMessage();
		}
		return "success";
		
	}
	public function getListParent($id)
	{
		$Cat = $this->select();
		$Cat->where('id = ?',$id);
		$dataCat = $this->fetchRow($Cat)->toArray();
		$data = array();
		if($dataCat['parent_id']!='0')
		{
			$data[] = $id;
			$data = array_merge($data,$this->getListParent($dataCat['parent_id']));
		}
		else 
		{
			$data[] = $id;
		}
		return $data;
		
		
	}
	static function getParent($id)
	{
			$excute = new QSource_System_Models_ArticleCat();
			$result = $excute->select();
			$result->where('id = ?',$id);
			$data = $excute->fetchRow($result)->toArray();
			return $data['parent_id'];
	}
	
    public function updateItem($data,$id){
		try {
		    $this->update($data, "id = $id");
		} 
		catch (Exception $e) {
		    return $e->getMessage();
		}
		return "success";
	}
}